
function pkgInstalled (rows){
	var retrows = new Array();

	for(var j=0;j<rows.length;j++){
		for (var u=0; u<rows[j].Repository.length; u++){
			if(rows[j].Repository[u].trim() == "Installed") {
				retrows.push(rows[j]);
			}
		}
	}

	document.getElementById("totalInstalled").innerHTML=retrows.length;
	return retrows;
}

function pkgNotInstalled (rows){
	var retrows = new Array();
	for(var j=0;j<rows.length;j++){
		var found = true;
		for (var u=0; u<rows[j].Repository.length;u++){
			if(rows[j].Repository[u].trim()=="Installed") {
				found = false;
			}
		}
		if (found) retrows.push(rows[j]);
	}
	document.getElementById("totalAvailables").innerHTML=retrows.length;
	return retrows;
}

var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;

function mouseOver(){
	var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
	var dsoctop=document.all? iebody.scrollTop : pageYOffset;
	var myparent=this.parentNode;
	var mydiv = document.createElement("div");
	var data = this.data;
	mydiv.id="div_"+this.id;
	mydiv.style.position="absolute";
	mydiv.style.top=10+dsoctop+"px";
//	mydiv.style.top=100+"px";
	mydiv.style.right="25px";
	mydiv.style.width="600px";
	mydiv.style.border="1px solid black";
	mydiv.style.backgroundColor="#FFFFFF";
	var pkg = "<table>";
	for (var i in data){
//		alert(i);
		if (data[i].length > 0){
			pkg = pkg+"<tr><td>"+ i +" : ";
			if (typeof(data[i])=="array"){
				pkg = pkg + data[i].toString();
			} else {
				pkg = pkg + data[i]
			}
			pkg = pkg +"</td></tr>";
		}
	}
	pkg = pkg + "</table>";
	mydiv.innerHTML=pkg;
	document.body.appendChild(mydiv);
}	

function mouseOut(){
	var elId = "div_"+this.id;
	var mydiv = document.getElementById(elId);
	if (mydiv) document.body.removeChild(mydiv);
}	
function add_repo(el){
	var mydiv = document.createElement("div");
	var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
	var dsoctop=document.all? iebody.scrollTop : pageYOffset;
	mydiv.style.position="absolute";
	mydiv.style.width="400px";
	mydiv.style.height="400px";
	mydiv.style.top=dsoctop+"px";
	mydiv.style.left="100px";
	mydiv.style.border="1px solid black";
	mydiv.style.backgroundColor="#FFFFFF";
	mydiv.innerHTML="Esta es la ventana para agregar repository";
	document.body.appendChild(mydiv);
}
function availableOnClick(){
	var str ="";
	var toInstall = findDepends(this.data,xwrt[this.storeId].rows,xwrt.GridInstallList.data);

	var str = "";
	var space = 0;
	var download = 0;
	var strError = "";
	for ( var i=0; i<toInstall.length; i++ ){
		if(toInstall[i].Error){
			strError += toInstall[i].Parent+toInstall[i].Error+toInstall[i].Package+"\n and It not found\n\nTHIS PACKAGE CAN NOT BE INSTALLED";
		}
	}
	if (strError.length>0){
		alert(strError);
		return;
	}
	document.getElementById(this.id).style.display="none";
	for (var i=0; i<toInstall.length; i++){
		row = xwrt.GridInstallList.insertData(toInstall[i]);
		if(i<toInstall.length-1){
			document.getElementById("GridInstallList_row:"+row).style.display="none";
		} else {
			addEvent(document.getElementById("GridInstallList_row:"+row),"mouseover",showDepends);
		}
	}

	for (var i=0; i<xwrt.GridInstallList.data.length; i++){
		var rown = xwrt.GridAvailables.data.fieldFind("Package",xwrt.GridInstallList.data[i].Package);
		document.getElementById("GridAvailables_row:"+rown).style.display="none";
		space += parseFloat(xwrt.GridInstallList.data[i]["Installed-Size"]);
		download += parseFloat(xwrt.GridInstallList.data[i]["Size"]);
	}
	document.getElementById("installedSize").innerHTML="Installed-Size "+space;
	document.getElementById("downloadSize").innerHTML="Download-Size "+download;
}
function installedOnClick(){
	var str ="";
	var pkg = this.data.Package;
	var isBase = false;
	if (this.data.Section){
		if (this.data.Section.trim() == "base"){
			isBase = true;
		}
	} else {
		var reg = xwrt[this.storeId].rows.fieldFind("Package",this.data.Package);
		if (reg !== false){
			if (xwrt[this.storeId].rows[reg].Section=="base") isBase = true;
		}
	}
	if(isBase) str = "<<< @TR<<big_warning|WARNING>> >>> \n\n@TR<<system_ipkg_Package|Package>> \"" + pkg + "\" @TR<<should not be removed>>!\n\n >>> @TR<<Removing may brick your router.>> <<< \n\n @TR<<System requires>> \"" + pkg + "\" @TR<<package to run.>> ";
	if(this.data.Repository.find("squashfs") && !isBase ){
		str = str + "This package is installed in squashfs.\n\nRemove it do not free memory in your router.\n\nRemove any way?";
	}
	if (isBase){
		alert(str);
	} else {
		var response = confirm(str);
	}
}
function findDepends(pkg,data,toInstallList,parent){
	var depends = new Array();
	var toInstall = new Array();
	var myParent = (parent) ? parent : new Array();
	myParent.push(pkg.Package);
	if (pkg.Depends){
		depends = pkg.Depends.split(", ");
		var version = -1;
		var checkVersion = null;
		for(var i=0; i<depends.length; i++){
			var version = depends[i].indexOf("(=");
			if (version>-1){
				version=[];
				version = depends[i].split(" (=");
				depends[i] = version[0].trim();
				checkVersion = version[1].replace(")","").trim();
			}
			var reg = data.fieldFind("Package", depends[i]);
			if (reg === false){
				toInstall.push({Error: " Depends from Package ", Package: depends[i]} );
				continue;
			}
			if(data[reg].Repository.find("Installed")===false){
				if (checkVersion){
					if (data[reg].Version != checkVersion){
						toInstall.push({Error: "Wrong Version", Package: depends[i], Version: data[reg].Version, RequireVersion: checkVersion} );
						continue;
					}
				}
				if (data[reg].Depends){
					var moreDep = findDepends(data[reg],data,toInstallList,myParent.slice());
					for (var n=0; n<moreDep.length; n++){
						if (moreDep[n].Repository.find("Installed")===false){
							var allready = toInstallList.fieldFind("Package",moreDep[n].Package);
							if (allready===false){
								allready = toInstall.fieldFind("Package",moreDep[n].Package)
								if (allready===false){
									toInstall.push(moreDep[n]);
								}
							}
						}
					}
				}
				if (data[reg].Repository.find("Installed")===false){
					var allready = toInstallList.fieldFind("Package",data[reg].Package);
					if (allready===false){
						allready = toInstall.fieldFind("Package",data[reg].Package);
						if (allready===false){
							toInstall.push(data[reg]);
//						} else {
//							alert("Ya esta en toInstall "+data[reg].Package+"\n"+toInstall[allready].Package+"\n"+myParent);
						}
					} else {
						for (var q=0;q<myParent.length;q++){
							if (typeof(toInstallList[allready].Parent)=="undefined") toInstallList[allready].Parent = [];
							if (toInstallList[allready].Parent.find(myParent[q])===false){
								toInstallList[allready].Parent.push(myParent[q]);
							}
						}
					}
				}
			}
		}
	}
	if (pkg.Repository.find("Installed")===false){
		for (var i=0;i<toInstall.length;i++){
			if(typeof(toInstall[i].Parent)=="undefined") toInstall[i].Parent = new Array();
			if(toInstall[i].Parent.find(pkg.Package) === false){
				for(var n=0;n<myParent.length;n++){
					if(toInstall[i].Parent.find(myParent[n])===false) {
						toInstall[i].Parent.push(myParent[n]);
					}
				}
			}
		}
		var allready = toInstallList.fieldFind("Package",pkg.Package);
		if (allready===false){
			allready = toInstall.fieldFind("Package",pkg.Package);
			if (allready===false){
				toInstall.push(pkg);
			}
		}
	}
	return toInstall;
}
function showDepends() {
	var newData = new Array();
	for(var i=0; i<xwrt.GridInstallList.data.length; i++){
		if(xwrt.GridInstallList.data[i].Parent){
			var found = xwrt.GridInstallList.data[i].Parent.find(this.data.Package.trim());
			if (found !== false){
				newData.push(xwrt.GridInstallList.data[i]);
			}
		}
	}
	xwrt.GridDepends.data = newData;
	xwrt.GridDepends.resetTable();
}

function cancelIsnstall(){
	var newData = new Array();
	var str = "";
	var elId = "div_"+this.id;
	var mydiv = document.getElementById(elId);
	if (mydiv) document.body.removeChild(mydiv);
	newData.push(this.data.Package)
	cancelList(this.data.Package,newData);
//	alert(newData.length+"\n"+newData);
	for (var i=0; i<newData.length; i++){
		var rown = xwrt.GridAvailables.data.fieldFind("Package",newData[i]);
		document.getElementById("GridAvailables_row:"+rown).style.display="block";
		rown = xwrt.GridInstallList.data.fieldFind("Package",newData[i]);
		xwrt.GridInstallList.data.splice(rown,1);
		var mytabla = document.getElementById("GridInstallList_table");
		mytabla.deleteRow(rown);
	}
//	alert(mytabla.rows.length);
	var space = 0;
	var download = 0;
	for (var i=0; i<xwrt.GridInstallList.data.length; i++){
//		alert(xwrt.GridInstallList.data[i].Package);
		var rown = xwrt.GridAvailables.data.fieldFind("Package",xwrt.GridInstallList.data[i].Package);
		space += parseFloat(xwrt.GridInstallList.data[i]["Installed-Size"]);
		download += parseFloat(xwrt.GridInstallList.data[i]["Size"]);
	}
	document.getElementById("installedSize").innerHTML="Installed-Size "+space;
	document.getElementById("downloadSize").innerHTML="Download-Size "+download;
//	xwrt.allInstallList.data = xwrt.GridInstallList.data;
//	xwrt.allInstallList.resetTable();
	xwrt.GridDepends.data = [];
	xwrt.GridDepends.resetTable();
//	xwrt.GridInstallList.reShowData();
//	xwrt.GridInstallList.resetTable();
//	document.getElementById("centro").innerHTML = str;
//	this.resizeGridCols();
}

function cancelList(pkg_name,delList){
	var newData = new Array();
	for(var i=0; i<xwrt.GridInstallList.data.length; i++){
		if(xwrt.GridInstallList.data[i].Parent){
			var found = xwrt.GridInstallList.data[i].Parent.find(pkg_name);
			if (found !== false){
				newData.push(xwrt.GridInstallList.data[i]);
			}
		}
	}
//	alert(newData.length);
	for(var a=0;a<newData.length;a++){
		if(newData[a].Parent && newData[a].Parent.length > 0){
			var found = newData[a].Parent.find(pkg_name);
//alert("Pkg:"+pkg_name+"\nGrid Pkg:"+newData[a].Package+"\nCount:"+newData[a].Parent.length+"\nArray:"+newData[a].Parent+"\n"+found);
			if (found !== false){
				newData[a].Parent.splice(found,1);
			}
		}
	}
	
	for (var b=0;b<newData.length;b++){
		pkg_name = newData[b].Package;
		for(var a=0;a<newData.length; a++){
			if(newData[a].Parent && newData[a].Parent.length > 0){
				var found = newData[a].Parent.find(pkg_name);
//alert(pkg_name+"\nGridPackage :"+newData.data[a].Package+"\nParent :"+newData.data[a].Parent);
				if (found !== false){
					newData[a].Parent.splice(found,1);
				}
//alert(pkg_name+"\nGridPackage :"+newData.data[a].Package+"\nParent :"+newData.data[a].Parent);
			}
		}
	}

	for (var a=0;a<newData.length;a++){
		var b = xwrt.GridInstallList.data.fieldFind("Package",newData[a].Package);
		if (b!==false) xwrt.GridInstallList.data[b].Parent = newData[a].Parent;
		if (newData[a].Parent.length==0){
			delList.push(newData[a].Package);
		}
	}
}

function cancelList2(pkg_name,delList){
	for(var a=0;a<xwrt.GridDepends.data.length; a++){
		var b = xwrt.GridInstallList.data.fieldFind("Package",xwrt.GridDepends.data[a].Package);
		if (b!==false){
			if(xwrt.GridInstallList.data[b].Parent){
				var found = xwrt.GridInstallList.data[b].Parent.find(pkg_name);
//				alert("pkg :"+pkg_name+"\n"+found+"\nGridPkg :"+xwrt.GridInstallList.data[b].Package+"\nParents :"+xwrt.GridInstallList.data[b].Parent)
				if (found !== false){
					xwrt.GridInstallList.data[b].Parent.splice(found,1);
					if(xwrt.GridInstallList.data[b].Parent.length == 0){
						delList.push(xwrt.GridInstallList.data[b].Package);
						cancelList(xwrt.GridInstallList.data[b].Package,delList);
//					} else {
//						cancelList(xwrt.GridInstallList.data[b].Package,delList);
					}
				}
			}
		}
	}
}

function cancelList1(pkg_name,delList){
	for(var a=0;a<xwrt.GridInstallList.data.length; a++){
		if(xwrt.GridInstallList.data[a].Parent){
			var found = xwrt.GridInstallList.data[a].Parent.find(pkg_name);
			if (found !== false){
//				xwrt.GridInstallList.data[a].Parent = xwrt.GridInstallList.data[a].Parent.splice(found,1);
//				alert(found+"\n"+xwrt.GridInstallList.data[a].Package+"\n"+xwrt.GridInstallList.data[a].Parent);
				xwrt.GridInstallList.data[a].Parent.splice(found,1);
//				alert(xwrt.GridInstallList.data[a].Parent.length+" "+xwrt.GridInstallList.data[a].Parent);
				if(xwrt.GridInstallList.data[a].Parent.length == 0){
//					alert(xwrt.GridInstallList.data[a].Package+"\n"+delList);
					delList.push(xwrt.GridInstallList.data[a].Package);
					cancelList(xwrt.GridInstallList.data[a].Package,delList);
				} else {
//					alert(xwrt.GridInstallList.data[a].Package+"\n"+delList);
					cancelList(xwrt.GridInstallList.data[a].Package,delList);
				}
			} else {
//				alert(pkg_name+"\n"+xwrt.GridInstallList.data[a].Package+"\n"+xwrt.GridInstallList.data[a].Parent);
			}
		}
	}
//	alert(pkg_name+"\n"+delList);
}
